package com.atguigu.edupublisher.controller;

import com.atguigu.edupublisher.service.EduService;
import com.atguigu.edupublisher.utils.DateFormatUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.Collection;
import java.util.Map;
import java.util.Set;

@RestController
@RequestMapping("/sugar")
public class OrderController {

    @Autowired
    private EduService eduService;

    @RequestMapping("/gmv")
    public String getGmv(@RequestParam(value = "date", defaultValue = "0") int date) {

        if (date == 0) {
            date = getToday();
        }

        System.out.println(date);

        return "{\n" +
                "  \"status\": 0,\n" +
                "  \"msg\": \"\",\n" +
                "  \"data\": " + eduService.getGmv(date) + "\n" +
                "}";
    }

    @RequestMapping("/course")
    public String getGmvByTm(@RequestParam(value = "date", defaultValue = "0") int date,
                             @RequestParam(value = "limit", defaultValue = "10") int limit) {

        if (date == 0) {
            date = getToday();
        }

        //查询数据
        Map<String, BigDecimal> gmvByCourse = eduService.getGmvByCourse(date, limit);

        Set<String> courseName = gmvByCourse.keySet();
        Collection<BigDecimal> courseGmv = gmvByCourse.values();

        return "{\n" +
                "  \"status\": 0,\n" +
                "  \"msg\": \"\",\n" +
                "  \"data\": {\n" +
                "    \"categories\": [\"" + StringUtils.join(courseName, "\",\"") + "\"],\n" +
                "    \"series\": [\n" +
                "      {\n" +
                "        \"name\": \"GMV:\",\n" +
                "        \"data\": [\n" +
                StringUtils.join(courseGmv, ",") + "\n" +
                "]\n" +
                "      }\n" +
                "    ]\n" +
                "  }\n" +
                "}";
    }






    private int getToday() {
        return Integer.parseInt(DateFormatUtil.toDate(System.currentTimeMillis()).replace("-", ""));
    }
}
